<?php

/**
 * Project form base class.
 *
 * @package    IPLIFE
 * @subpackage form
 * @author     alexey.shapran@gmail.com
 * @version    SVN: $Id: sfDoctrineFormBaseTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
abstract class BaseFormDoctrine extends sfFormDoctrine
{
	public function __construct($object = null, $options = array(), $CSRFSecret = null)
	{
		parent::__construct($object,$options,$CSRFSecret);

		//tell the widget schema which fields are required
		$this->widgetSchema->addOption(
			'required_fields',
			$this->getRequiredFields()
		);

		$this->widgetSchema->addFormFormatter('table',
			new RequiredLabelsFormatterTable($this->widgetSchema)
		);
	}

	protected function getRequiredFields(sfValidatorSchema $validatorSchema = null, $format = null)
	{
	    if (is_null($validatorSchema))
	    {
	      $validatorSchema = $this->validatorSchema;
	    }
	
	    if (is_null($format))
	    {
	      $format = $this->widgetSchema->getNameFormat();
	    }
	
	    $fields = array();
	
	    foreach ($validatorSchema->getFields() as $name => $validator)
	    {
	      $field = sprintf($format, $name);
	      if ($validator instanceof sfValidatorSchema)
	      {
	        // recur
	        $fields = array_merge(
	          $fields,
	          $this->getRequiredFields($validator, $field.'[%s]')
	        );
	      }
	      else if ($validator->getOption('required'))
	      {
	        // this field is required
	        $fields[] = $field;
	      }
	    }

		return $fields;
	}

	public function setup(){}

}
